package com.ims.controller;

import com.ims.util.*;
import com.ims.pojo.User;
import com.ims.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 新增管理员用户，返回用户id
     * @param user
     * @return
     */
    @PostMapping("/add")
    public Result<Integer> add(@RequestBody User user) {
        return userService.add(user);
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @DeleteMapping("/deleteById")
    public Result deleteById(Integer userId){
        return userService.deleteById(userId);
    }

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping("/findAll")
    public Result<List<User>> findAll() {
        return userService.findAll();
    }

    @PostMapping("/findPage")
    public Result<PageResult> findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult<User> pageResult = userService.findPage(queryPageBean);
        return new Result<>(true, StatusCode.OK, "分页查询成功", pageResult);
    }

    @GetMapping("/findById")
    public Result<User> findById(Integer userId) {
        User user = userService.getById(userId);
        return new Result<>(true, StatusCode.OK, "查询成功", user);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PostMapping("/update")
    public Result updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestParam(name = "username") String username, @RequestParam(name = "password") String password, HttpServletRequest request, HttpServletResponse response) {
        return userService.login(username, password, request, response);
    }

    /**
     * 退出登录
     * @param userId
     * @return
     */
    @PostMapping("/logout")
    public Result logout(@RequestParam(name = "id") Integer userId) {
        return userService.logout(userId);
    }
}
